﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Model;
using MySql.Data.MySqlClient;

namespace DAL
{
    public class SOP_SendSNSDAL
    {
        /// <summary>
        /// 手机短信记录
        /// </summary>
        /// <param name="Mobile"></param>
        /// <param name="VerifyCode"></param>
        /// <returns></returns>
        public int Add(String BossID, String Mobile, String VerifyCode, int SNSType, int Status)
        {
            using (MySqlConnection conn = SqlHelper.Settings.GetSOPMySqlConnection(true))
            {
                try
                {
                    MySqlCommand cmd = new MySqlCommand("P_AddSendSNS_API", conn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@BossID_Para", MySqlDbType.VarChar, 9).Value = BossID;
                    cmd.Parameters.Add("@Mobile_Para", MySqlDbType.VarChar, 15).Value = Mobile;
                    cmd.Parameters.Add("@VerifyCode_Para", MySqlDbType.VarChar, 10).Value = VerifyCode;
                    cmd.Parameters.Add("@SNSType_Para", MySqlDbType.Int32).Value = SNSType;
                    cmd.Parameters.Add("@Status_Para", MySqlDbType.Int32).Value = Status;
                    cmd.Parameters.Add("@AddTime_Para", MySqlDbType.DateTime).Value = System.DateTime.Now;
                    cmd.Parameters.Add("@IsSuccess_Para", MySqlDbType.Int32).Value = 0;
                    cmd.Parameters["@IsSuccess_Para"].Direction = ParameterDirection.Output;
                    if (conn.State == ConnectionState.Closed)
                    {
                        conn.Open();
                    }
                    cmd.ExecuteNonQuery();
                    Object ObjRtID = cmd.Parameters["@IsSuccess_Para"].Value;
                    return ObjRtID == null || ObjRtID.ToString() == string.Empty ? -5 : (int)ObjRtID;

                }
                catch (Exception ex)
                {
#if DEBUG
                    throw ex;
#else
                    return -10000;
#endif
                }
                finally
                {
                    conn.Close();
                }
            }
        }


        /// <summary>
        /// 验证验证码
        /// </summary>
        /// <returns>5代表存在</returns>
        public int VerifyCode(String Mobile, String VerifyCode, out string error)
        {
            error = "";
            #region 读取缓存
            try
            {
                //缓存手机验证码1分钟
                string cacheKey = "ARCSOFTCRAPIVerifyCode$" + Mobile + "$" + VerifyCode;
                object obj = Common.MemCachedHelper.GetCache(cacheKey);
                if (obj != null)
                    return 5;
            }
            catch (Exception ex)
            {
                error = ex.Message.ToString();
            }
            #endregion

            #region DB
            using (MySqlConnection conn = SqlHelper.Settings.GetSOPMySqlConnection(false))
            {
                try
                {
                    MySqlCommand cmd = new MySqlCommand("P_GetSendSNSByVerifyCode_API", conn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@Mobile_Para", MySqlDbType.VarChar, 15).Value = Mobile;
                    cmd.Parameters.Add("@VerifyCode_Para", MySqlDbType.VarChar, 10).Value = VerifyCode;
                    cmd.Parameters.Add("@CurrentDate_Para", MySqlDbType.DateTime).Value = System.DateTime.Now;
                    cmd.Parameters.Add("@IsSuccess_Para", MySqlDbType.Int32).Value = 0;
                    cmd.Parameters["@IsSuccess_Para"].Direction = ParameterDirection.Output;
                    if (conn.State == ConnectionState.Closed)
                    {
                        conn.Open();
                    }
                    cmd.ExecuteNonQuery();
                    Object ObjRtID = cmd.Parameters["@IsSuccess_Para"].Value;
                    return ObjRtID == null || ObjRtID.ToString() == string.Empty ? -5 : (int)ObjRtID;

                }
                catch (Exception ex)
                {
                    error = ex.Message.ToString();
#if DEBUG
                    throw ex;
#else
                    return -10000;
#endif
                }
                finally
                {
                    conn.Close();
                }
            }
            #endregion
        }
    }
}
